package com.csglxt.view.action;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csglxt.base.BaseAction;
import com.csglxt.domain.Goods;
import com.csglxt.domain.GoodsCate;
import com.csglxt.domain.Storage;
import com.csglxt.utils.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @ClassName: GoodsAction
 * @Description: TODO(商品管理控制器 （管理商品信息的添加、删除、修改,搜索）)
 * @author Itchbeilef
 * @date 2015-5-28 下午6:10:03
 * 
 */
@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class GoodsAction extends BaseAction<Goods> {
    private String Searchname;
    private String GoodsCatename;

    /**
     * 
     * @Title: list
     * @Description: TODO(商品分页搜索列表展示)
     * @param
     * @return list视图
     * @throws
     */
    public String list() {
        QueryHelper qureyHelper = new QueryHelper(Goods.class, "g")
                .addOrderProperty("name", true);
        if (Searchname != null && !Searchname.isEmpty()) {
            qureyHelper.addCondition(" g.name like ? ", "%" + Searchname + "%");
        }
        if (GoodsCatename != null && !GoodsCatename.isEmpty()
                && !GoodsCatename.equals("0")) {
            qureyHelper.addCondition(" g.goodsCate.id = ? ", GoodsCatename);
        }
        qureyHelper.preparePageBean(goodsService, pageNum, pageSize);
        Catelist();
        return "list";
    }

    /**
     * 
     * @Title: Catelist
     * @Description: TODO(商品所有分类信息存进request域用于select标签显示)
     * @param
     * @return
     * @throws
     */
    private void Catelist() {
        List<GoodsCate> GoodsCateList = goodsCateService.findAll();
        ActionContext.getContext().put("GoodsCateList", GoodsCateList);
    }

    /**
     * 
     * @Title: CateSearchlist
     * @Description: TODO(商品分类 分页搜索列表展示)
     * @param
     * @return Catelist视图
     * @throws
     */
    public String CateSearchlist() {
        QueryHelper qureyHelper = new QueryHelper(GoodsCate.class, "g");
        if (Searchname != null && !Searchname.isEmpty()) {
            qureyHelper.addCondition(" g.name like ? ", "%" + Searchname + "%");
        }
        qureyHelper.addOrderProperty("name", true);
        qureyHelper.preparePageBean(goodsCateService, pageNum, pageSize);
        return "Catelist";
    }

    /**
     * 
     * @Title: saveGoodsUI
     * @Description: TODO(返回添加商品界面,调用了Catelist())
     * @param
     * @return
     * @throws
     */
    public String saveGoodsUI() {
        Catelist(); //
        return "saveGoodsUI";
    }

    /**
     * 
     * @Title: editGoodsUI
     * @Description: TODO(返回修改商品界面 存进了当前修改商品实体到值栈)
     * @param
     * @return
     * @throws
     */
    public String editGoodsUI() {
        Catelist();
        getValueStack().push(goodsService.getById(model.getId()));
        return "saveGoodsUI";
    }

    /**
     * 
     * @Title: addGoods
     * @Description: TODO(添加商品到数据库)
     * @param
     * @return 重定向到商品列表
     * @throws
     */
    public String addGoods() {
        if (model != null && GoodsCatename != null && !GoodsCatename.isEmpty()) {
            model.setGoodsCate(goodsCateService.getById(GoodsCatename));
            goodsService.save(model);
        }
        return "redirectlist";
    }

    /**
     * 
     * @Title: upGoods
     * @Description: TODO(修改的商品信息更新到数据库)
     * @param
     * @return
     * @throws
     */
    public String upGoods() {
        if (model != null && !model.getId().isEmpty()
                && !model.getName().isEmpty()) {
            model.setGoodsCate(goodsCateService.getById(GoodsCatename));
            goodsService.update(model);
        }
        return "redirectlist";
    }

    /**
     * 
     * @Title: delGoods
     * @Description: TODO(删除商品操作更新到数据库,注意所有引用商品表的其他表项被删除或失去关联)
     * @param
     * @return
     * @throws
     */
    public String delGoods() {
        if (model != null) {
            goodsService.delete(model.getId());
        }
        return list();
    }

    /**
     * 
     * @Title: ajxaddtoStorage
     * @Description: TODO(异步处理 添加此商品信息到库存,库存中添加一条此商品为0的库存项,并返回添加结果到用户)
     * @param
     * @return
     * @throws
     */
    public void ajxaddtoStorage() throws IOException {
        if (model != null && model.getId() != null) {
            Goods goods = goodsService.getById(model.getId());
            HttpServletResponse response = (HttpServletResponse) ActionContext
                    .getContext().get(
                            org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
            response.setContentType("text/html;charset=utf-8");
            if (storageService.queryStorageByGoods(goods).size() == 0) {
                storageService.save(new Storage(goods, new BigDecimal(0)));
                response.getWriter().print("成功添加到库存");
            } else {
                response.getWriter().print("库存中已存在");
            }
        }
    }

    public void ajx() {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        Goods good = new Goods();
        /*
         * GoodsCate goodsCate= good.getGoodsCate();
         * goodsCate.setId(GoodsCatename);
         */
        good.setGoodsCate(new GoodsCate(GoodsCatename));
        good.setName(model.getName());
        Goods goods = (Goods) goodsService.findByname(good);
        if (goods != null) {
            // 重点用是用print输出的数据格式是json
            try {
                response.getWriter().print(
                        "{\"success\":true,\"msg\":\"商品已存在,如需添加请更改类别\"}");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {

            try {
                response.getWriter().print("{\"success\":false,\"msg\":\"\"}");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // get set 方法
    public String getSearchname() {
        return Searchname;
    }

    public void setSearchname(String searchname) {
        Searchname = searchname;
    }

    public String getGoodsCatename() {
        return GoodsCatename;
    }

    public void setGoodsCatename(String goodsCatename) {
        GoodsCatename = goodsCatename;
    }
}
